package com.jic.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.product.enums.ProductTypeEnum;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.mapper.MHotProductMapper;
import com.jic.product.mapper.MProductMapper;
import com.jic.product.model.entity.MProduct;
import com.jic.product.model.vo.response.MProductTypeResponse;
import com.jic.product.service.MProductService;
import lombok.extern.slf4j.Slf4j;
import com.jic.product.manage.request.ShopInfoRequest;
import com.jic.product.manage.response.ShopInfoResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
 * @Classname 田雨
 * @Description TODO
 * @Created by 22720
 * 业务层
 */

@Service
@Slf4j
public class MProductServiceImpl implements MProductService {

    @Resource
    private MProductMapper mapper;


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 新增记录
     */
    @Override
    public int save(MProduct request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 新增或修改记录
     */
    @Override
    public int saveOrUpdate(MProduct request){
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION,"入参为空");
            }
            if (request.getId() != null) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if(flag == 1){
                    return 1;
                }
            }else{
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if(flag == 1){
                    return 1;
                }else{
                    throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"用户保存或修改失败");
        }
        return -1;
    }

    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 删除记录
     */
    @Override
    public int deleteByPrimaryKey(MProduct request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /*
      逻辑删除记录
     */
    @Override
    public int deleteByPrimaryKeyLogically(MProduct request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }

    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改记录
     */
    @Override
    public int updateByPrimaryKeySelective(MProduct request){
        try {
            log.info("修改记录");
            MProduct param = new MProduct();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            }else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改记录失败");
        }

    }

    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 单表查询详情信息
     */
    @Override
    public MProduct getByPrimaryKey(MProduct request) {
        return mapper.selectByPrimaryKey(request);
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     *  分页查询
     */
    @Override
    public PageInfo<MProduct> listPageBySelective(MProduct request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MProduct> list = mapper.listPageBySelective(request);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<MProduct> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return new PageInfo(list);
    }

	/*
	    查询下架商品
	 */

    @Override
    public PageInfo<MProduct> listPageX(MProduct request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MProduct> list = mapper.listPageX(request);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<MProduct> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return new PageInfo(list);
    }

    /*
	    查询上架商品
	 */

    @Override
    public PageInfo<MProduct> listPageS(MProduct request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MProduct> list = mapper.listPageS(request);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<MProduct> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return new PageInfo(list);
    }

    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * M_PRODUCT 查询列表（不分页）
     */
    @Override
    public List<MProduct> listAllRecord(MProduct request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    /**
     * 查询 [商品表]包含的商品类型
     * @return
     */
    @Override
    public RestResult<List<MProductTypeResponse>> queryProductType() {
        List<String> listType = mapper.queryProductType();
        List<MProductTypeResponse> responseList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(listType)) {
            listType.forEach(type -> {
                MProductTypeResponse response = new MProductTypeResponse();
                if (StringUtils.isEmpty(type)) {
                    response.setType(null);
                    response.setTypeName("无");
                } else {
                    ProductTypeEnum productTypeEnum = ProductTypeEnum.getByCode(Integer.valueOf(type));
                    if (null == productTypeEnum) {
                        response.setType(type);
                        response.setTypeName(type);
                    } else {
                        response.setType(type);
                        response.setTypeName(productTypeEnum.getMessage());
                    }
                }
                responseList.add(response);
            });
        }
        return RestResult.success(responseList);
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改为下架
     */
    @Override
    public int updateA(MProduct param) {
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"传入参数为空");
        }
        int up = mapper.updateA(param.getId());
        if (up!=1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"上架失败");
        }
        return 1;
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改为上架
     */
    @Override
    public int updateB(MProduct param) {
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"传入参数为空");
        }
        int up = mapper.updateB(param.getId());
        if (up!=1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"下架失败");
        }
        return 1;
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改协议上浮
     */
    @Override
    public int updateListA(MProduct param) {
        if (param.getId() == null && param.getAgreementOnFloatingAtio()==null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"传入参数为空");
        }
        Integer se = mapper.selectListA(param.getId());
        if (se == null){
            int sa = mapper.save(param);
            if (sa == 1){
                return 1;
            }
        }
        int up = mapper.updateListA(param);
        if (up!=1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"协议上浮修改失败");
        }
        return 1;
    }



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改微众价
     */

    @Override
    public int updateListB(MProduct param) {
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"传入参数为空");
        }
        Integer se = mapper.selectListA(param.getId());
        if (se == null){
            int sa = mapper.save(param);
            if (sa == 1){
                return 1;
            }
        }
        int up = mapper.updateListB(param);
        if (up!=1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"微众价修改失败");
        }
        return 1;
    }

    @Override
    public PageInfo<MProduct> topTenProductQuery(MProduct request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" sale_num desc LIMIT 10 ");
        List<MProduct> list = mapper.topTenProductQuery(request);

        PageInfo pageInfo = new PageInfo(list);
        PageResult<MProduct> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return new PageInfo(list);
    }
    /**
    *@Auther: YGH
    *@DATE: 2020/12/1
     *【查询积分可兑换商品/通过品牌查询商品】
    */
    @Override
    public PageInfo<ShopInfoResponse> listPointsProduct(ShopInfoRequest shopInfoRequest, int pageNum, int pageSize) {
        log.info("H5 -> 查询积分可兑换商品/通过品牌查询商品开始！入参：{}",shopInfoRequest);
        PageHelper.startPage(pageNum, pageSize);
        List<ShopInfoResponse> list = mapper.listPointsProduct(shopInfoRequest);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<ShopInfoResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        log.info("H5 -> 查询积分可兑换商品/通过品牌查询商品结束！出参：{}",list);
        return new PageInfo(list);
    }

}
